<?php

require_once 'cfg.php';
include_once 'lib/includeHub.php';

class questionPage {

    var $questions;

    function __construct($sort, $skip, $numresults) {
        global $dbhost, $dbuser, $dbpasswd, $dbschema;
        $this->questions = array();
        $dbc = mysqli_connect($dbhost, $dbuser, $dbpasswd, $dbschema);
        $querytype = mysqli_real_escape_string($dbc, $querytype);
        $skip = mysqli_real_escape_string($dbc, $skip);
        $numresults = mysqli_real_escape_string($dbc, $numresults);
        $sql = "SELECT `upvotes`, `downvotes`, `title`, `views`, `tags`, `idquestions`, `posterid`, `hasacceptedans` FROM `questions` ORDER BY $sort LIMIT $skip, $numresults";
        $result = mysqli_query($dbc, $sql);
        $i = 0;
        while ($row = mysqli_fetch_array($result)) {
            $this->questions[$i][title] = $row ['title'];
            $this->questions[$i][votes] = $row ['upvotes'] - $row ['downvotes'];
            $this->questions[$i][views] = $row ['views'];
            $this->questions[$i][qid] = $row ['idquestions'];
            $this->questions[$i][op] = get_object_vars(new user($row ['posterid']));
            $this->questions[$i][ans] = $row ['hasacceptedans'];
            $i++;
        }
        mysqli_close($dbc);
    }

    function displayQuestionList() {
        print(json_encode($this->questions));
    }

}

class question {

    var $title;
    var $upvotes;
    var $downvotes;
    var $answers;
    var $op;
    var $tags;
    var $qid;
    var $views;

    function __construct($qid) {
        global $dbhost, $dbuser, $dbpasswd, $dbschema;
        $dbc = mysqli_connect($dbhost, $dbuser, $dbpasswd, $dbschema);
        $qid = mysqli_real_escape_string($dbc, $qid);
        $sql = "SELECT `upvotes`, `downvotes`, `title`, `views`, `tags`, `idquestions`, `posterid`, `text` FROM `questions` WHERE `questions`.`idquestions` = $qid LIMIT 1";
        $result = mysqli_query($dbc, $sql);
        $qdata = mysqli_fetch_row($result);
        //echo var_dump($qdata);
        $this->title = $qdata [2];
        $this->text = $qdata [7];
        $this->upvotes = $qdata [0];
        $this->downvotes = $qdata [1];
        $this->op = new user($qdata [6], $dbhost, $dbuser, $dbpasswd, $dbschema);
        $this->tags = new tags(explode(' ', $qdata [4]));
        $this->qid = $qdata [5];
        $this->comments = new qcommentlist($qid);
        $this->answerlist = new answerlist($qid);
        $this->answers = $this->answerlist->answers;
        unset($this->answerlist);
        mysqli_close($dbc);
    }

    function displayQuestion() {
        $array = get_object_vars($this);
        print(json_encode($array));
    }

}

class answerlist {

    var $upvotes;
    var $downvotes;
    var $text;
    var $posterid;

    function __construct($qid) {
        global $dbhost, $dbuser, $dbpasswd, $dbschema;
        $dbc = mysqli_connect($dbhost, $dbuser, $dbpasswd, $dbschema);
        $qid = mysqli_real_escape_string($dbc, $qid);
        $sql = "SELECT `answers`.`upvotes`, `answers`.`downvotes`, `answers`.`text`, `answers`.posterid`, `answers`.`idanswers` FROM `answers` WHERE `answers`.`question`= $qid";

        $result = mysqli_query($dbc, $sql);
        $i = 0;
        if ($result) {
            while ($row = mysqli_fetch_array($result)) {
                $this->upvotes[$i] = $result['upvotes'];
                $this->downvotes [$i] = $result ['downvotes'];
                $this->text [$i] = $result ['text'];
                $this->posterid [$i] = $result ['posterid'];
                $this->aid[$i] = $result['idanswers'];
                $this->comments[$i] = new acommentlist($result['idanswers']);
                $i++;
            }
        }
        mysqli_close($dbc);
    }

    

}

class avotebox {

    var $aid;
    var $upvotes;
    var $downvotes;
    var $row;

    function __construct($aid, $user, $dbhost, $dbuser, $dbpasswd, $dbschema) {
        $sql = "SELECT `avotes`.`up`, `answers`.`upvotes`, `answers`.`downvotes`, `answers`.`idanswers`  FROM avotes INNER JOIN answers ON `avotes`.`answer`=`answer`.`idanswers` WHERE `avotes`.`answer`=$aid AND `avotes`.`userid`=$user LIMIT 2";
        $dbc = mysqli_connect($dbhost, $dbuser, $dbpasswd, $dbschema);
        $result = mysqli_query($dbc, $sql);
        $this->row = mysqli_fetch_row($result);
        $this->upvotes = $this->row['upvotes'];
        $this->downvotes = $this->row['downvotes'];
        $this->aid = $aid;
        if (mysqli_num_rows() > 1) {
            throw new exception("Too many votes per user--objects.php->avotebox->__construct( $aid , $user )");
        }
        // __Bugcheck: Vote duplicate
    }

    function display() {


        echo '<div style="float:left"><table><tr><td>';
        if ($this->row) {
            if ($this->row['up']) {
                echo "<img src=\"rsrc/upvote_after.svg\" width=48 height=48 onclick=\"castAnsVote($this->aid, 'upoff'); \" />";
                echo "</td></tr><tr><td>";
                echo "<span style=\"color: green;\">+$this->upvotes</span></td></tr>";
                echo "</td></tr><tr><td>";
                echo "<span style=\"color: red; font-size: x-large;\">+$this->upvotes</span></td></tr>";
                echo "<img src=\"rsrc/downvote_before.svg\" width=48 height=48 onclick\"castAnsVote($this->aid, 'downon'); \" />";
            } else {
                echo "<img src=\"rsrc/upvote_before.svg\" width=48 height=48 onclick=\"castAnsVote($this->aid, 'upon'); \" />";
                echo "</td></tr><tr><td>";
                echo "<span style=\"color: green;\">+$this->upvotes</span></td></tr>";
                echo "</td></tr><tr><td>";
                echo "<span style=\"color: red; font-size: x-large;\">+$this->upvotes</span></td></tr>";
                echo "<img src=\"rsrc/downvote_after.svg\" width=48 height=48 onclick\"castAnsVote($this->aid, 'downoff'); \" />";
            }
        } else {
            echo "<img src=\"rsrc/upvote_before.svg\" width=48 height=48 onclick=\"castAnsVote($this->aid, 'upon'); \" />";
            echo "</td></tr><tr><td>";
            echo "<span style=\"color: green;\">+$this->upvotes</span></td></tr>";
            echo "</td></tr><tr><td>";
            echo "<span style=\"color: red; font-size: x-large;\">+$this->upvotes</span></td></tr>";
            echo "<img src=\"rsrc/downvote_before.svg\" width=48 height=48 onclick\"castAnsVote($this->aid, 'downon'); \" />";
        }
        echo '</tr></td></table></div>';
    }

}

class qvotebox {

    var $qid;
    var $upvotes;
    var $downvotes;
    var $row;

    function __construct($qid, $user, $dbhost, $dbuser, $dbpasswd, $dbschema) {
        $sql = "SELECT `qvotes`.`type`, `questions`.`upvotes`, `questions`.`downvotes`, `questions`.`idquestions`  FROM `qvotes` INNER JOIN questions ON `qvotes`.`question`=`answer`.`idanswers` WHERE `qvotes`.`question`=$qid AND `qvotes`.`userid`=$user LIMIT 2";
        $dbc = mysqli_connect($dbhost, $dbuser, $dbpasswd, $dbschema);
        $result = mysqli_query($dbc, $sql);
        if ($result) {
            $this->row = mysqli_fetch_row($result);
            $this->upvotes = $this->row['upvotes'];
            $this->downvotes = $this->row['downvotes'];
            $this->qid = $qid;
            if (mysqli_num_rows() > 1) {
                throw new Exception("Too many votes per user--objects.php->qvotebox->__construct( $qid , $user )");
            }
        } else {
            $sql = "SELECT  `questions`.`upvotes`, `questions`.`downvotes`  FROM `questions` WHERE `questions`.`idquestions`=$qid";
            $result = mysqli_query($dbc, $sql);

            $this->row = mysqli_fetch_row($result);
            $this->upvotes = $this->row[0];
            $this->downvotes = $this->row[1];
            $this->qid = $qid;
        }
        // __Bugcheck: Vote duplicate
    }

    function display() {
        echo '<table><tr><td>';
        if ($this->row) {
            if (@$this->row['up'] && @$this->row['up'] == 1) {
                echo "<tr><td><img src=\"rsrc/upvote_after.png\" width=48 height=48 onclick=\"castQuesVote($this->qid, 'upoff'); \" />";
                echo "</td></tr><tr><td>";
                echo "<span style=\"color: green; font-size: x-large;\">+$this->upvotes</span>";
                echo "</td></tr><tr><td>";
                echo "<span style=\"color: red; font-size: x-large;\">+$this->downvotes</span></td></tr>";
                echo "<tr><td><img src=\"rsrc/downvote_before.png\" width=48 height=48 onclick=\"castQuesVote($this->qid, 'downon'); \" />";
            }
            if (@$this->row['up'] && @$this->row['up'] == 0) {
                echo "<tr><td><img src=\"rsrc/upvote_before.png\" width=48 height=48 onclick=\"castQuesVote($this->qid, 'upon'); \" />";
                echo "</td></tr><tr><td>";
                echo "<span style=\"color: green; font-size: x-large;\">+$this->upvotes</span>";
                echo "</td></tr><tr><td>";
                echo "<span style=\"color: red; font-size: x-large;\">+$this->downvotes</span></td></tr>";
                echo "<tr><td><img src=\"rsrc/downvote_after.png\" width=48 height=48 onclick=\"castQuesVote($this->qid, 'downoff'); \" />";
            }
        } if (!isset($this->row['up'])) {
            echo "<tr><td><img src=\"rsrc/upvote_before.png\" width=48 height=48 onclick=\"castQuesVote($this->qid, 'upon'); \" />";
            echo "</td></tr><tr><td>";
            echo "<span style=\"color: green; font-size:x-large\">+$this->upvotes</span>";
            echo "</td></tr><tr><td>";
            echo "<span style=\"color: red; font-size: x-large;\">+$this->downvotes</span></td></tr>";
            echo "<tr><td><img src=\"rsrc/downvote_before.png\" width=48 height=48 onclick=\"castQuesVote($this->qid, 'downon'); \" />";
        }
    }

}

class user {

    function __construct($userid, $dbhost, $dbuser, $dbpasswd, $dbschema) {
        $dbc = mysqli_connect($dbhost, $dbuser, $dbpasswd, $dbschema);
        //TODO
        $this->username = 'foo';
    }

}

class qcommentlist {
    // __TODO__
}

class acommentlist {
    // __TODO__
}

class tags {

    var $tagarray;
    protected $taglinkarray;
    public $taglinks;

    function __construct($tagarray) {
        $taglinks = '<a href="';
        array_walk($tagarray, 'tags::tagarray_walk');
    }

    function tagarray_walk($curvalue, $idx) {
        $this->taglinks = '';
        $this->taglinks = $this->taglinks . "<span class=\"tag\"><a href=\"tags.php?tag=$curvalue\">$curvalue</a></span>";
    }

}

?>